package com.whmskj.xjlhsc.panel.service.imp;

import com.whmskj.xjlhbc.db.DataSource;
import com.whmskj.xjlhbc.db.DataSourceType;
import com.whmskj.xjlhsc.panel.mapper.ProduceMapper;
import com.whmskj.xjlhsc.panel.pojo.entity.*;
import com.whmskj.xjlhsc.panel.pojo.vo.MachinenumberVO;
import com.whmskj.xjlhsc.panel.pojo.vo.ProcessListVO;
import com.whmskj.xjlhsc.panel.pojo.vo.ProduceVO;
import com.whmskj.xjlhsc.panel.service.IProduceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author jiaruchun
 * @date 2025-09-13 16:05
 */
@Slf4j
@Service
public class ProduceServiceImpl implements IProduceService {
    @Autowired
    private ProduceMapper produceMapper;

    @Override
    public ProduceVO getProduceData(String process) {
        ProduceVO produceVO = new ProduceVO();
        //出勤--
        List<Attendance> attendanceList = produceMapper.getAttendanceList();
        if(attendanceList.isEmpty()){
            attendanceList.add(new Attendance("前纺",0,0,0));
            attendanceList.add(new Attendance("细纱",0,0,0));
            attendanceList.add(new Attendance("络筒",0,0,0));
        }
        produceVO.setAttendanceList(attendanceList);
        //出勤分布--
        AttendanceDistribution attendanceDistribution = produceMapper.getAttendanceDistribution();
        List<AttendanceList> attendanceList1 = new ArrayList<>();
        attendanceList1.add(new AttendanceList("出勤",attendanceDistribution.getAttendanceRatio()));
        attendanceList1.add(new AttendanceList("缺勤",attendanceDistribution.getAbsenceRatio()));
        attendanceList1.add(new AttendanceList("离职",attendanceDistribution.getDismissalRatio()));
        attendanceList1.add(new AttendanceList("休息",attendanceDistribution.getRestRatio()));
        produceVO.setAttendanceDistribution(attendanceList1);
        //产量能耗占比--
        List<ProduceEnergyRatio> produceEnergyRatioList = produceMapper.getProduceEnergyRatioList(process);
        //产量除以1000化为吨
        for (ProduceEnergyRatio produceEnergyRatio : produceEnergyRatioList) {
            Double produce = produceEnergyRatio.getProduce();
            produceEnergyRatio.setProduce(produce / 1000f);
        }
        produceVO.setProduceEnergyRatioList(produceEnergyRatioList);
        //各工序产量--
        List<ProduceByProcess> produceByProcessList = produceMapper.getProduceByProcessList(process);
        produceVO.setProduceByProcessList(produceByProcessList);
        return produceVO;
    }



    @Override
    @DataSource(DataSourceType.SC)
    public List<MachinenumberVO> getMachinenumberList() {
        List<MachinenumberVO> machinenumberList = produceMapper.getMachinenumberList();
        log.info("getMachinenumberList: {}", machinenumberList);
        //去除其中''包裹的字符串
        for (MachinenumberVO machinenumberVO : machinenumberList) {
            String machineNumber = machinenumberVO.getMachineNumber();
            if(machineNumber.startsWith("'") && machineNumber.endsWith("'")){
                machinenumberVO.setMachineNumber(machineNumber.substring(1,machineNumber.length()-1));
            }
        }
        return machinenumberList;
    }

    @Override
    public List<ProcessListVO> getProcessList() {
        List<ProcessListVO> processList = produceMapper.getProcessList();
        for (ProcessListVO processListVO : processList) {
            if(processListVO.getProcess().equals("末并,头并")) {
                processListVO.setProcess("末并");
            }
        }
        processList.removeIf(process -> process.getProcess().equals("清花"));
        return processList;
    }
}
